]>
Commit | Line | Data |
---|---|---|
f8aec187 BB |
1 | using System; |
2 | using System.Collections.Generic; | |
3 | using System.Linq; | |
4 | using System.Text; | |
5 | using Microsoft.Xna.Framework; | |
6 | using Microsoft.Xna.Framework.Graphics; | |
7 | ||
8 | namespace SuperPolarity | |
9 | { | |
10 | static class ActorManager | |
11 | { | |
38c7d3f9 BB |
12 | |
13 | static Game Game; | |
14 | static int OutlierBounds; | |
f8aec187 BB |
15 | static List<Actor> Actors; |
16 | ||
17 | static ActorManager() | |
18 | { | |
38c7d3f9 | 19 | OutlierBounds = 100; |
f8aec187 BB |
20 | Actors = new List<Actor>(); |
21 | } | |
22 | ||
23 | static public void CheckIn(Actor actor) | |
24 | { | |
25 | Actors.Add(actor); | |
26 | } | |
27 | ||
28 | static public void CheckOut(Actor actor) | |
29 | { | |
30 | Actors.Remove(actor); | |
31 | } | |
32 | ||
33 | static public void Update(GameTime gameTime) | |
34 | { | |
2af83e98 | 35 | CheckActors(); |
38c7d3f9 | 36 | CheckOutliers(); |
f8aec187 BB |
37 | foreach (Actor actor in Actors) |
38 | { | |
39 | actor.Update(gameTime); | |
40 | } | |
41 | } | |
42 | ||
43 | static public void Draw(SpriteBatch spriteBatch) | |
44 | { | |
45 | foreach (Actor actor in Actors) | |
46 | { | |
47 | actor.Draw(spriteBatch); | |
48 | } | |
49 | } | |
2af83e98 BB |
50 | |
51 | static void CheckActors() | |
52 | { | |
53 | var i = 0; | |
54 | foreach (Actor actor in Actors) | |
55 | { | |
56 | i++; | |
57 | foreach (Actor other in Actors.Skip(i)) | |
58 | { | |
59 | CheckCollision(actor, other); | |
60 | ||
61 | if (actor.GetType().IsSubclassOf(typeof(Ship)) && other.GetType().IsSubclassOf(typeof(Ship))) | |
62 | { | |
63 | CheckMagnetism((Ship)actor, (Ship)other); | |
64 | } | |
65 | } | |
66 | } | |
67 | } | |
68 | ||
69 | static void CheckCollision(Actor actor, Actor other) | |
70 | { | |
71 | ||
72 | } | |
73 | ||
74 | static void CheckMagnetism(Ship actor, Ship other) | |
75 | { | |
76 | if (actor.CurrentPolarity != Ship.Polarity.Neutral && other.CurrentPolarity != Ship.Polarity.Neutral) | |
77 | { | |
78 | var dy = other.Position.Y - actor.Position.Y; | |
79 | var dx = other.Position.X - actor.Position.X; | |
80 | var linearDistance = Math.Sqrt(Math.Pow(dx, 2) + Math.Pow(dy, 2)); | |
81 | var angle = (float) Math.Atan2(dy, dx); | |
38c7d3f9 | 82 | var otherAngle = (float)Math.Atan2(-dy, -dx); |
2af83e98 BB |
83 | |
84 | if (linearDistance < actor.MagneticRadius || linearDistance < other.MagneticRadius) | |
85 | { | |
86 | actor.Magnetize(other, (float)linearDistance, angle); | |
38c7d3f9 | 87 | other.Magnetize(actor, (float)linearDistance, otherAngle); |
2af83e98 BB |
88 | } |
89 | } | |
90 | } | |
38c7d3f9 BB |
91 | |
92 | static void CheckOutliers() | |
93 | { | |
94 | for (var i = Actors.Count; i > 0; i--) | |
95 | { | |
96 | var actor = Actors[i-1]; | |
97 | if (actor.Position.X < -OutlierBounds || actor.Position.Y < -OutlierBounds || | |
98 | actor.Position.X > Game.GraphicsDevice.Viewport.Width + OutlierBounds || | |
99 | actor.Position.Y > Game.GraphicsDevice.Viewport.Height + OutlierBounds) | |
100 | { | |
101 | CheckOut(actor); | |
102 | } | |
103 | } | |
104 | } | |
105 | ||
106 | internal static void SetGame(Game game) | |
107 | { | |
108 | Game = game; | |
109 | } | |
f8aec187 BB |
110 | } |
111 | } |